package logic

import (
	"context"
	"sz2/C/api/internal/svc"
	"sz2/C/api/internal/types"
	"sz2/C/api/pkg"
	"sz2/C/rpc/good"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.GoodRpc.Login(l.ctx, &good.LoginRequest{
		Phone:    req.Phone,
		Password: req.Password,
		SmsCode:  req.SmsCode,
	})
	if err != nil {
		return &types.Response{
			Msg:  err.Error(),
			Code: 500,
			Data: nil,
		}, nil
	}
	token, err := pkg.GetJwtToken(int(login.Id))
	if err != nil {
		return &types.Response{
			Msg:  err.Error(),
			Code: 500,
			Data: nil,
		}, nil
	}
	return &types.Response{
		Msg:  "登录成功",
		Code: 200,
		Data: token,
	}, nil
}
